package com.zyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.dao.UserMapper;
import com.zyg.pojo.EchartsData;
import com.zyg.pojo.Permission;
import com.zyg.pojo.User;
import com.zyg.pojo.UserRole;
import com.zyg.service.PermissionService;
import com.zyg.service.UserRoleService;
import com.zyg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private PermissionService permissionService;
    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    @Transactional
    public boolean add(User user) {
        int result = userMapper.add(user);
        // 获取新增用户的主键
        int userId = userMapper.findIdByUser(user);
        // 遍历处理角色数据
        // 往user_role 插入数据
        Integer[] roleIds = user.getRoleIds();
        List<UserRole> userRoleList = new ArrayList<>();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUId(userId);
                userRole.setRId(roleId);
                userRoleList.add(userRole);
            }
            userRoleService.saveBatch(userRoleList);
        }
        return result > 0;
    }

    @Override
    @Transactional
    public boolean update(User user) {

        int result = userMapper.update(user);
        userRoleService.remove(new QueryWrapper<UserRole>().eq("u_id",user.getId()));

        Integer[] roleIds = user.getRoleIds();
        List<UserRole> userRoleList = new ArrayList<>();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUId(user.getId());
                userRole.setRId(roleId);
                userRoleList.add(userRole);
            }
            userRoleService.saveBatch(userRoleList);
        }
        return result > 0;
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public List<EchartsData> getDeptIndexData() {
        return userMapper.getDeptIndexData();
    }


    @Override
    public List<Permission> getMenuList() {
        List<Permission> menuList = permissionService.getMenuList();
        return menuList;
    }

    @Override
    public List<Permission> getMenuListByUserId(Integer loginId) {
        List<Permission> menuList = permissionService.getMenuListByUserId(loginId);
        return menuList;
    }
}